#coding=utf-8
import unittest
from appium import webdriver
from selenium import webdriver
from time import sleep
from nav_plugin_op import nav_plugin_factory
from Tool.usefulTools import userfulToolsFactory
from selenium.common.exceptions import WebDriverException
import os
from Public import BasePage
from Public.BasePage import element_Oprate
from Public.BasePage import BasicAction
from Public import BaseConstant
import settings

navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()
element_operate_instances = element_Oprate()
element_find_instance = BasicAction()
Verifycheckt_Paramter_instances = BaseConstant.Verifycheckt_Paramter
dealHelper_parameter = BaseConstant.DisposeCase

nav_name = ["导航栏工作",'xpath',"//android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.FrameLayout/android.widget.RadioButton"]
plugin_name = ["插件列表-我的任务",'name','我的任务']
myTastChck = ["我的任务页面显示",'name','未完成']

tool_photoadd_id = "cn.com.egova.pluginreport:id/tool_photoadd" #上传多媒体的---相册 ID


class RecVerifyCheck():

    def open_Verify_Check_plug(self,driver,file_path):

        try:
            sleep(5)
            # 点击工作的导航，打开工作列表
            element_operate_instances.element_click(nav_name,file_path)
            # 点击"我的任务"的插件，打开插件
            element_operate_instances.element_click(plugin_name,file_path)
            #等待我的任务页面内容进行加载，加载完成之后进行页面确认并界面
            sleep(5)
            element_operate_instances.element_save_pic(file_path, 'open_Verify_Check_plug_successful')
            # element_operate_instances.element_click(Verifycheckt_Paramter_instances.exit_name, file_path)
        except WebDriverException,error:
            print error
            element_operate_instances.element_save_pic(file_path, 'open_Verify_Check_plug_fail')
            raise

    def Verify_rec_task(self,file_path):

        try:
            #点击核实按钮，进入核实任务页面
            sleep(2)
            if element_operate_instances.element_display_commit(Verifycheckt_Paramter_instances.verify_function_button,file_path):
                element_operate_instances.element_click(Verifycheckt_Paramter_instances.verify_check_button, file_path)
            else:
                print u'未找到核实按钮，核实失败'
                raise
            element_operate_instances.element_save_pic(settings.pic_file_path,'Verify_rec_task_detail_1')
            #选择图片进行上传
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.media_view_id, file_path)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.tool_photoadd_id, file_path)
            sleep(2)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.verifycheck_first_photo_select_button, file_path)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.select_photo_id, file_path)
            sleep(2)
            #选择核实结果：属实
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.verified_id, file_path)
            #输入核实描述
            element_operate_instances.element_send_keys(Verifycheckt_Paramter_instances.verify_desc_location,Verifycheckt_Paramter_instances.verify_desc,settings.pic_file_path)
            #点击上报按钮
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.report_btn_id, file_path)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.confirm_name, file_path)
            sleep(3)
            element_operate_instances.element_save_pic(file_path, 'Verify_the_task_successful')
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.close_name, file_path)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.exit_Verify_name, file_path)
            #返回列表
            element_operate_instances.element_click(dealHelper_parameter.dealhelper_back_button,file_path)

        except WebDriverException,error:
            print error
            element_operate_instances.element_save_pic(file_path, 'Verify_the_task_fail')
            raise

    def Check_Rec_task(self,file_path):

        try:
            #点击核查按钮，进入核查任务页面
            sleep(2)
            if element_operate_instances.element_display_commit(Verifycheckt_Paramter_instances.check_function_button,file_path):
                element_operate_instances.element_click(Verifycheckt_Paramter_instances.verify_check_button, file_path)
            else:
                print u'未找到核查按钮，核实失败'
                raise
            sleep(3)
            element_operate_instances.element_save_pic(settings.pic_file_path,'Check_Rec_task_detail_1')
            #进行上传图片
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.media_view_id, file_path)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.tool_photoadd_id, file_path)
            sleep(2)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.verifycheck_first_photo_select_button, file_path)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.select_photo_id, file_path)
            sleep(2)
            #选择核查结果：已处理
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.verified_id, file_path)
            #进行上报
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.report_btn_id, file_path)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.confirm_name, file_path)
            sleep(3)
            #点击关闭按钮，并返回我的任务
            element_operate_instances.element_save_pic(file_path, 'Verification_task_successful')
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.close_name, file_path)
            sleep(3)
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.exit_Verification_name, file_path)

        except WebDriverException,error:
            print error
            element_operate_instances.element_save_pic(file_path, 'Verification_task_fail')
            raise

    #任务类型筛选
    def task_type_filter(self,type):
        # 点击筛选按钮
        element_operate_instances.element_click(Verifycheckt_Paramter_instances.filter_id, settings.pic_file_path)
        if type =='verify':
            #    选择核实任务
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.verify_name, settings.pic_file_path)
        else:
            element_operate_instances.element_click(Verifycheckt_Paramter_instances.check_name, settings.pic_file_path)

    #点击列表的第一条案件
    def verifycheckt_first_rec_click(self):
		sleep(2)
		#获取当前第一条案件的任务号
		task_num = element_find_instance.find_element(Verifycheckt_Paramter_instances.verifycheck_task_num,settings.pic_file_path).get_attribute('text')
		print u'当前操作的案件号为： '+str(task_num)
		# 点击打开搜索结果--第一条案件
		element_operate_instances.element_click(Verifycheckt_Paramter_instances.verifycheck_task_num,settings.pic_file_path)
        #返回task_num
		return task_num



RecVerifyCheck_instances = RecVerifyCheck()

class Verify_Checkt_Module():
    #核实任务
    def Verify_Rec_Task(self):
        #进行类型筛选
        RecVerifyCheck_instances.task_type_filter('verify')
        sleep(5)
        #进行截图，查看核查列表的列表数据加载是否正确
        element_operate_instances.element_save_pic(settings.pic_file_path,'check_rec_task_list_load')
        try:
            #打开第一条案件
            task_num = RecVerifyCheck_instances.verifycheckt_first_rec_click()
            # 案件详情页面打开之后截图
            element_operate_instances.element_save_pic(settings.pic_file_path,'verify_first_rec_detail' + task_num)
            #进行核实操作
            RecVerifyCheck_instances.Verify_rec_task(settings.pic_file_path)
        except Exception,error:
            print u'无可用的核查任务，抛出异常，需要重新测试'
            raise

    #核查任务
    def Check_Rec_Task(self):
        #检查是否在任务列表
        while (True):
            if element_operate_instances.element_display_commit(Verifycheckt_Paramter_instances.filter_id, settings.pic_file_path):
                print u'当前在核实核查任务列表界面，可进行操作'
                break
            else:
                print u'当前不在核实核查任务列表界面，先调用2次返回操作'
                try:
                    element_operate_instances.element_click(dealHelper_parameter.dealhelper_back_button,settings.pic_file_path)
                except Exception,e:
                    print e
                    print u'多执行了次返回操作'
        #进行类型筛选
        RecVerifyCheck_instances.task_type_filter('check')
        try:
            #打开第一条案件
            task_num = RecVerifyCheck_instances.verifycheckt_first_rec_click()
            # 案件详情页面打开之后截图
            element_operate_instances.element_save_pic(settings.pic_file_path,'check_first_rec_detail' + task_num)
            # 进行核查操作
            RecVerifyCheck_instances.Check_Rec_task(settings.pic_file_path)
        except Exception,error:
            print u'无可用的核查任务，抛出异常，需要重新测试'
            raise



if __name__ == '__main__':
    unittest.main()
